package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.cache.service.IAnswerCacheService;
import cn.wolfcode.luowowo.cache.service.IQuestionCacheService;
import cn.wolfcode.luowowo.cache.vo.AnswerStats;
import cn.wolfcode.luowowo.cache.vo.QuestionStats;
import cn.wolfcode.luowowo.comment.domain.AnswerComment;
import cn.wolfcode.luowowo.comment.domain.QuestionComment;
import cn.wolfcode.luowowo.comment.service.IAnswerCommentService;
import cn.wolfcode.luowowo.comment.service.IQuestionCommentService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

public class QuestionStatsPersistenceJob {
    @Reference
    private IQuestionCacheService questionCacheService;
    @Reference
    private IQuestionCommentService questionCommentService;
    @Reference
    private IAnswerCacheService answerCacheService;
    @Reference
    private IAnswerCommentService answerCommentService;

    @Scheduled(cron = "0/10 * * * * ?")
    public void persistence(){
        //核心:从redis中查出数据,同步到mysql中
        //从redis查询所有提问的信息
        List<QuestionStats> vos = questionCacheService.listAllQuestion();
        //遍历vos,将其封装成文章攻略对象
        for (QuestionStats vo : vos) {
            QuestionComment question=new QuestionComment();
            question.setId(vo.getQId());
            question.setViewnum(vo.getViewnum());
            question.setReplynum(vo.getReplynum());
            question.setThumbsupnum(vo.getThumbsupnum());
            question.setSharenum(vo.getFavornum());   //提问中的分享数,对应关注中的关注数
           //更新到mongoDB
            questionCommentService.updateStats(question);

          //把提问对应的回答跟新到mongoDB
            List<AnswerStats> list = answerCacheService.listAnswerByQid(vo.getQId());
            for (AnswerStats answerStats : list) {
                AnswerComment answerComment = new AnswerComment();
                answerComment.setId(answerStats.getAId());
                answerComment.setUserId(answerStats.getUserId());
                answerComment.setQId(vo.getQId());
                answerComment.setThumbupnum(answerStats.getThumbsupnum());
                answerComment.setLevel(answerStats.getLevel());
                answerComment.setUsername(answerStats.getUsername());
                answerComment.setHeadUrl(answerStats.getHeadUrl());

                answerCommentService.updateStats(answerComment);
            }
        }








    }
}
